package com.andy.dialog

import androidx.databinding.ViewDataBinding

/**
 * Author:Andy
 * Date:2023/3/24 18:47
 * Description:CommonDialog
 */
class CommonDialog : BaseBindingDialog<ViewDataBinding>() {
    private var mOnDataCallbackListener: OnDataCallbackListener? = null
    private var mOnStyleCallBackListener: OnStyleCallBackListener? = null
    private var mLayoutId = 0

    companion object {
        fun init(): CommonDialog {
            return CommonDialog()
        }
    }

    override fun initVMData() {
        mOnDataCallbackListener?.dataCallback()
    }

    override fun initStyle() {
        mOnStyleCallBackListener?.styleCallback()
    }

    override fun getLayoutId(): Int {
        return mLayoutId
    }

    //获取ViewDataBinding
    fun <VB : ViewDataBinding> getDataBinding(): VB {
        return mBinding as VB
    }


    //设置layout
    fun addLayout(mLayoutId: Int): CommonDialog {
        this.mLayoutId = mLayoutId
        return this
    }

    //初始化数据
    fun <VB : ViewDataBinding> bind(block: (bind: VB) -> Unit): CommonDialog {
        setDataCallBackListener(object : OnDataCallbackListener {
            override fun dataCallback() {
                block.invoke(getDataBinding())
            }
        })
        return this
    }

    //初始化数据
    fun set(block: () -> Unit): CommonDialog {
        setDataCallBackListener(object : OnDataCallbackListener {
            override fun dataCallback() {
                block.invoke()
            }
        })
        return this
    }

    //设置样式
    fun style(style: () -> Unit): CommonDialog {
        setStyleCallBackListener(object : OnStyleCallBackListener {
            override fun styleCallback() {
                style.invoke()
            }
        })
        return this
    }


    private fun setDataCallBackListener(mOnDataCallbackListener: OnDataCallbackListener) {
        this.mOnDataCallbackListener = mOnDataCallbackListener
    }

    private fun setStyleCallBackListener(mOnStyleCallBackListener: OnStyleCallBackListener) {
        this.mOnStyleCallBackListener = mOnStyleCallBackListener
    }

}